import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:get/get_connect/http/src/utils/utils.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:getwidget/getwidget.dart';
import 'package:isolate_manager/isolate_manager.dart';
import 'package:nb_utils/nb_utils.dart';
import 'package:pitchertools/http/dio_http.dart';
import 'package:pitchertools/models/fans_query_bean.dart';
import 'package:pitchertools/pages/query_fans/widgets/dialogs/update_query_fans_msg_dialog.dart';
import 'package:velocity_x/velocity_x.dart';

import 'index.dart';
import 'widgets/dialogs/add_query_fans_msg_dialog.dart';
import 'widgets/dialogs/details_query_fans_msg_dialog.dart';
import 'widgets/widgets.dart';

class QueryFansPage extends GetView<QueryFansController> {
  const QueryFansPage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    List<FansQueryBean> fansQueryBeanList =
        controller.state.fansQueryBeanListRx ?? [];

    return Column(
      children: [
        const GFCard(
          padding: EdgeInsets.all(2),
          margin: EdgeInsets.all(8),
          content: Row(
            children: [
              Expanded(
                flex: 2,
                child: Text(
                  "客服号",
                  textAlign: TextAlign.center,
                ),
              ),
              Expanded(
                flex: 1,
                child: Text(
                  "粉",
                  textAlign: TextAlign.center,
                ),
              ),
              Expanded(
                flex: 1,
                child: Icon(Icons.trending_up),
              ),
              Expanded(
                flex: 2,
                child: Text(
                  "操作",
                  textAlign: TextAlign.center,
                ),
              ),
            ],
          ),
        ),
        Obx(
          () => ListView.builder(
            itemCount: fansQueryBeanList.length,
            itemBuilder: (context, index) {
              var item = fansQueryBeanList[index];
              var simpleNumber = item.csNumber.length <= 4
                  ? item.csNumber
                  : item.csNumber.substring(item.csNumber.length - 4);

              return GestureDetector(
                onTap: () {
                  showDialog(
                    context: context,
                    barrierDismissible: true,
                    builder: (BuildContext context) =>
                        DetailsQueryFansMsgDialog(selectedIndex: index),
                  );
                },
                child: GFCard(
                  padding: const EdgeInsets.all(2),
                  margin: const EdgeInsets.all(8),
                  content: Row(
                    children: [
                      Expanded(
                        flex: 2,
                        child: Text(
                          simpleNumber,
                          // item.csNumber,
                          textAlign: TextAlign.center,
                        ).onTap(() async {
                          await Clipboard.setData(
                              ClipboardData(text: simpleNumber));
                          SmartDialog.showToast("简短客服号复制成功,双击查看详情");
                        }).onDoubleTap(() {
                          showDialog(
                            context: context,
                            barrierDismissible: true,
                            builder: (BuildContext context) =>
                                DetailsQueryFansMsgDialog(selectedIndex: index),
                          );
                        }),
                      ),
                      Expanded(
                        flex: 1,
                        child: Text(
                          item.addFansNow.toString(),
                          textAlign: TextAlign.center,
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: Text(
                          "-1",
                          textAlign: TextAlign.center,
                        ),
                      ),
                      Expanded(
                        flex: 2,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            GFIconButton(
                              type: GFButtonType.solid,
                              color: Colors.blue,
                              icon: const Icon(Icons.edit),
                              onPressed: () {
                                showDialog(
                                  context: context,
                                  barrierDismissible: true,
                                  builder: (BuildContext context) =>
                                      UpdateQueryFansMsgDialog(
                                          selectedIndex: index),
                                );
                              },
                            ),
                            3.widthBox,
                            GFIconButton(
                              type: GFButtonType.solid,
                              color: Colors.red,
                              icon: const Icon(Icons.delete),
                              onPressed: () {
                                fansQueryBeanList.removeAt(index);
                              },
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
          ).expand(),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<QueryFansController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
            title: const Text("批量查粉"),
            actions: [
              IconButton(
                icon: const Icon(Icons.add),
                onPressed: () async {
                  showDialog(
                    context: context,
                    barrierDismissible: true,
                    builder: (BuildContext context) => AddQueryFansMsgDialog(),
                  );
                },
              ).marginOnly(right: 10),
              IconButton(
                icon: const Icon(Icons.refresh),
                onPressed: () async {
                  controller.refreshFansData();
                },
              ).marginOnly(right: 10),
            ],
          ),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
